<template>
  <div class="chart-content-wrapper" @mouseenter="mouseenter()" @mouseleave="mouseleave()">
    <!-- 头部筛选和标题 -->
    <top-filter :title="reportName" ref="topFilter" :isShowHelp="this.isShowHelp" :helpContent="this.helpMessage">
      <double-check-filter :isParentLinkChild="true" @confirmDoubleSelect="planAreaSelect" defaultText="统筹区"
                           :filter="filterN" type="planArea"></double-check-filter>
    </top-filter>
    <!-- 图标和结论 -->
    <div class="chart-container" v-show="isShowChart">
      <div id="pension-expenditure-analysis-person" class="chart-type1">
      </div>
      <Summarize :showSummarizeText="conclusion"></Summarize>
    </div>
  </div>
</template>

<script>

  import TopFilter from '../../last/filter/top-filter1'
  import DoubleCheckFilter from '../../last/filter/double-check-filter.vue'
  import Summarize from '../../last/summarize/summarize.vue'
  import {setChartHeight, loadingOpt, isJanuary} from '../../../assets/js/utils.js'

  import {getLiNianGeRenYangLaoAnalysis} from '../../../api/api'

  export default {
    components: {
      TopFilter,
      DoubleCheckFilter,
      Summarize
    },
    props: ['isChange', 'filter'],
    data () {
      return {
        reportName: '历年个人养老保险待遇分析',
        filterN: JSON.parse(JSON.stringify(this.filter)),
        isShowChart: true,
        conclusion: '',
        areaCode: '',
        areaName: '',
        callBackParams: {},
        isShowHelp: false,
        helpMessage: ''
      }
    },
    mounted () {
      this.initAreaData();
      this.initMyChart();
      setChartHeight();

      this.getData();
    },
    activated () {
      if (!this.myChart) {
        this.initAreaData();
        this.initMyChart();
        setChartHeight();
      }
      this.getData();
    },
    watch: {
      isChange () {
        setTimeout(() => {
          if (this.myChart) {
            this.myChart.resize();
          }
        }, 111);
      }
    },
    methods: {
      initAreaData () {
        let areaCode = [];
        let areaName = [];
        this.filter.nc.planArea.map((item, index) => {
          if (index > 0 && index <= 6) {
            areaCode.push(item.selectId)
            areaName.push(item.selectName);
          }
        });
        this.areaCode = areaCode.join(',');
        this.areaName = areaName.join('、');
      },
      initMyChart () {
        this.myChart = this.$echarts.init(document.getElementById('pension-expenditure-analysis-person'));
        let that = this;
        this.myChart.on('legendselectchanged', function (params) {
          let option = this.getOption();
          let select_key = Object.keys(params.selected);
          if (!params.selected[params.name]) {
            select_key.map(res => {
              option.legend[0].selected[res] = true;
            });
          } else {
            select_key.map(res => {
              option.legend[0].selected[res] = true;
            });
            option.legend[0].selected[params.name] = true;
          }
          let legend = [];
          that.myChart.dispatchAction({
            type: 'legendselectchanged',
            batch: legend
          });
          that.myChart.setOption(option);
        });
        // 点击黑块的事件
        this.myChart.getZr().off('click');
        this.myChart.getZr().on('click', params => {
          let hash = window.location.hash;
          // 获取点击的点
          var pointInPixel = [params.offsetX, params.offsetY];
          // 判断点击的点是否在 gridId为 'two' 中
          if (this.myChart.containPixel({gridId: 'two'}, pointInPixel)) {

            var xIndex = this.myChart.convertFromPixel({seriesIndex: 0},
              [params.offsetX, params.offsetY])[0];
            // 事件处理代码书写位置
            let data = {
              hash,
              year: this.callBackParams.yearList[xIndex],
              areaCode: that.areaCode,
              areaName: that.areaName
            };
            that.$router.push({
              path: `/decision/pension-analysis/pension-expenditure-analysis-person-detail`,
              query: data
            });
          }
        });
      },
      planAreaSelect (selectedIdArr, selectedNameArr) {
        // 统筹区选择
        this.areaCode = selectedIdArr.join(',');
        this.areaName = selectedNameArr.join('、');
        this.getData();
      },
      getData () {
        if (this.myChart) {
          this.myChart.clear();
          this.myChart.showLoading(loadingOpt);
        }

        getLiNianGeRenYangLaoAnalysis('liNianYangLao/liNianGeRenYangLaoAnalysis', this.areaCode).then(response => {
          let json = response.data;
          if (response.code == 0) {
            this.isShowChart = true;
            this.myChart.hideLoading();

            this.reportName = json.reportName;
            this.conclusion = json.reportConclusion;
            this.helpMessage = json.tip;
            this.isShowHelp = this.helpMessage.length > 0;

            this.callBackParams = json;
            this.drawLine(json)
          } else {
            this.isShowChart = false;
          }
        }).catch(res => {
          this.isShowChart = false;
        })
      },
      drawLine (json) {
        let boundaryNum = isJanuary();

        let option = {
          grid: [//此参数可在一个dom中划分多个图表 可设置不同图表的位置
            {
              top: '10%',
              height: '14%',
              left: '60px',
              right: '60px',
              tooltip: {
                formatter: "{b}</br>{c}%" //给当前坐标系添加弹窗
              }
            },
            {
              top: '35%',
              height: '53%',
              left: '60px',
              right: '60px',
              id: 'two',
              tooltip: {
                show: true,
                trigger: 'axis',
                axisPointer: {
                  type: 'shadow'
                },
              }
            }
          ],
          legend: [
            {
              data: ['平均值增长率'],
              itemWidth: 10,
              itemHeight: 10,
              icon: 'circle',
              selectedMode: false,
              textStyle: {
                color: '#fff'
              },
            },
            {
              top: '95%',
              selectedMode: false,
              left: 'center',
              icon: 'rect',
              itemWidth: 10,
              itemHeight: 10,
              itemGap: 20,
              data: ['最小值',
                '中位数',
                '众数',
                // '最大值',
                {name: '平均值', icon: 'circle',}
              ],
              textStyle: {
                color: '#fff'
              },
            },
          ],
          tooltip: {
            show: true,
            textStyle: {
              fontSize: 12
            },
            formatter: function (params) {
              return `<span style="color: #fff">个人月度养老金待遇金额</span></br>
                      <span style="color: #fff">年份: ${params[0].name}</span></br>
                      <span style="color: #fff">最小值: ${params[0].data}元</span></br>
                      <span style="color: #fff">平均值: ${params[3].data}元</span></br>
                      <span style="color: #fff">中位数: ${params[1].data}元</span></br>
                      <span style="color: #fff">众数: ${params[2].data}元</span></br>
                      <span style="color: #fff">最大值: ${json.maximumList[params[0].dataIndex]}元</span></br>
                      <span style="color: #67ffdb;
                            text-align: center;
                            padding: 2px 10px;
                            line-height: 30px;height: 30px">单击图表查看详情</span>
`;
            }
          },
          xAxis: [
            {
              data: json.yearList,
              show: true,
              axisLine: {
                show: true, // 显示x轴坐标线并设置为虚线
                lineStyle: {
                  color: '#294363'
                }
              },
              axisTick: {
                show: false // 不显示x轴坐标的刻度
              },
              axisLabel: {
                show: false // 不显示x轴坐标数值
              }
            },
            {
              data: json.yearList,
              gridIndex: 1,
              axisLine: { // x轴的横线是否显示
                show: true,
                lineStyle: {
                  color: '#2a4565'
                }
              },
              axisTick: { // x轴的刻度短竖线是否显示
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          yAxis: [
            {
              splitLine: {
                show: false,
              },
              position: 'right',
              splitNumber: 2,
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            },
            {
              name: '单位 : 元',
              nameTextStyle: {
                color: '#fff',
              },
              splitLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                  type: 'dashed',
                }
              },
              gridIndex: 1,
              axisLine: {
                show: true,
                lineStyle: {
                  color: '#2a4565',
                }
              },
              axisTick: {
                show: false
              },
              axisLabel: {
                formatter: '{value}',
                textStyle: {
                  color: '#fff'
                }
              },
            }
          ],
          series: [ // 折线
            {
              type: 'line',
              showSymbol: true,
              name: '平均值增长率',
              data: json.avgRatio,
              symbol: 'circle',
              color: '#129cff',
              symbolSize: 10,
              tooltip: {
                show: true,
                formatter: function (params) {
                  return `<span style="color: #fff"}">平均值增长率</span></br>
                          <span style="color: #fff"">年份: ${params.name}</span></br>
                          <span style="color: #fff"'>增长率: ${params.value}%</span>`
                }
              },
              label: {
                show: true,
                position: 'top',
                formatter: '{c}%',
                color: '#fff'
              }
            },
            {
              name: '最小值',
              color: '#ff6a93',
              type: 'bar',
              data: json.minimumList,
              xAxisIndex: 1,
              yAxisIndex: 1,
            },
            {
              name: '中位数',
              color: '#ffd66a',
              type: 'bar',
              data: json.medianList,
              xAxisIndex: 1,
              yAxisIndex: 1,
            },
            {
              name: '众数',
              color: '#55cdb7',
              type: 'bar',
              data: json.modeList,
              xAxisIndex: 1,
              yAxisIndex: 1,
            },
            // {
            //   name: '最大值',
            //   color: '#0298df',
            //   type: 'bar',
            //   data: json.maximumList,
            //   xAxisIndex: 1,
            //   yAxisIndex: 1,
            // },
            {
              name: '平均值',
              color: '#9de5f3',
              type: 'line',
              data: json.avgList,
              xAxisIndex: 1,
              yAxisIndex: 1,
            }
          ],
        };

        this.myChart.setOption(option);
      },
      mouseenter () {
        this.$refs.topFilter._hideChartTitle()
      },
      mouseleave () {
        this.$refs.topFilter._showChartTitle()
      },
    }
  }
</script>

<style scoped>


</style>
